package host.haiwai.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import host.haiwai.entity.Role;
import host.haiwai.mapstruct.RoleMapstruct;
import host.haiwai.service.RoleServer;
import host.haiwai.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author joefche / JC
 * @since 2021-08-29
 */
@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleServer roleServer;

    //列表页面显示
    @GetMapping("/roleList")
    public String roleList() {
        return "/role/roleList";
    }

    //修改页面
    @GetMapping("/roleEdit")
    public ModelAndView roleEdit(@RequestParam(defaultValue = "0") Integer id, ModelAndView modelAndView) {
        modelAndView.addObject("id",id);
        modelAndView.setViewName("/role/roleEdit");
        return modelAndView;
    }

    //获取指定资料
    @GetMapping("/getRoleById")
    @ResponseBody
    public Object getUserById(@RequestParam(defaultValue = "0") Integer id) {
        Role role = roleServer.getById(id);
        RoleVo roleVo = RoleMapstruct.mapper.rolePojo2Vo(role);
        return JSON.toJSON(roleVo);
    }

    //显示新增页面
    @GetMapping("/roleAdd")
    public ModelAndView roleAdd(ModelAndView modelAndView) {
        modelAndView.setViewName("/role/roleAdd");
        return modelAndView;
    }

    //json数据列表
    @RequestMapping("/jsonList")
    @ResponseBody
    public Object jsonList(@RequestParam(defaultValue = "1") Integer page ,
                           @RequestParam(defaultValue = "1") Integer limit,
                           @RequestParam(defaultValue = "") String searchObject ) {
        QueryWrapper wrapper = new QueryWrapper<>();
        RoleVo roleQuery = JSON.parseObject(searchObject, RoleVo.class);

        if(roleQuery != null) wrapper.like("role_name",roleQuery.getRoleName());

        Page<Role> pageData = new Page<>(page,limit);
        Page<Role> role = roleServer.page(pageData, wrapper);
        List<Role> roleList = role.getRecords();
        List<RoleVo> roleVoList = RoleMapstruct.mapper.roleList2Vo(roleList);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",role.getTotal());
        map.put("data",roleVoList);
        return JSON.toJSON(map);

    }

    //新增操作
    @PostMapping("/roleAdd")
    @ResponseBody
    public Object roleAdd(Role role) {
        HashMap<Object, Object> map = new HashMap<>();
        boolean save = roleServer.save(role);
        if(save) {
            map.put("code",1);
            map.put("data","操作成功");
        } else {
            map.put("code", 2);
            map.put("data", "操作失败");
        }
        return JSON.toJSON(map);
    }

    //删除操作
    @GetMapping("/deleteRole")
    @ResponseBody
    public Object deleteRole(@RequestParam(defaultValue = "0") Integer id) {
        HashMap<Object, Object> map = new HashMap<>();
        boolean remove = roleServer.removeById(id);
        if(remove) {
            map.put("code",1);
            map.put("data","操作成功");
        } else {
            map.put("code", 2);
            map.put("data", "操作失败");
        }
        return JSON.toJSON(map);
    }

    //更新会员资料
    @PostMapping("/roleEdit")
    @ResponseBody
    public Object roleEdit(Role role) {
        HashMap<Object, Object> map = new HashMap<>();

        boolean update = roleServer.updateById(role);

        if(update) {
            map.put("code",1);
            map.put("data","操作成功");
        } else {
            map.put("code", 2);
            map.put("data", "操作失败");
        }
        return JSON.toJSON(map);
    }
}

